package com.sen.paydemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sen.paydemo.entity.OrderInfo;
import com.sen.paydemo.entity.Product;
import com.sen.paydemo.enums.OrderStatus;
import com.sen.paydemo.mapper.OrderInfoMapper;
import com.sen.paydemo.mapper.ProductMapper;
import com.sen.paydemo.service.OrderInfoService;
import com.sen.paydemo.util.OrderNoUtils;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.time.Instant;
import java.util.List;

/**
 * 订单信息Service实现
 *
 * @author 韩宗森
 * @since 2024-01-09 19:53:30
 */
@Service
@Slf4j
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Resource
    private ProductMapper productMapper;

    @Override
    public OrderInfo createOrderByProductId(Integer productId) {

        // 查找已存在但未支付的订单
        OrderInfo orderInfo = this.getNoPayOrderByProductId(productId);
        if (orderInfo != null) {
            return orderInfo;
        }

        // 获取商品信息
        Product product = productMapper.selectById(productId);

        // 生成订单
        orderInfo = new OrderInfo();
        orderInfo.setTitle(product.getTitle());//标题
        orderInfo.setOrderNo(OrderNoUtils.getOrderNo());//订单编号
        orderInfo.setProductId(productId);//产品id
        orderInfo.setTotalFee(product.getPrice()); // 订单金额 分
        orderInfo.setOrderStatus(OrderStatus.NOTPAY.getType());//订单状态

        // 存到数据库
        baseMapper.insert(orderInfo);

        return orderInfo;
    }

    @Override
    public void saveCodeUrl(String orderNo, String codeUrl) {
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no", orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setCodeUrl(codeUrl);
        // 根据条件更新订单
        baseMapper.update(orderInfo,queryWrapper);
    }

    // 根据订单号,更新支付状态
    @Override
    public void updateStatusByOrderNo(String orderNo, OrderStatus orderStatus) {

        log.info("更新订单状态 ==> {}",orderStatus.getType());
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);

        OrderInfo orderInfo = new OrderInfo();
        orderInfo.setOrderStatus(orderStatus.getType());
        // 更新
        baseMapper.update(orderInfo,queryWrapper);
    }

    @Override
    public String getOrderStatus(String orderNo) {

        QueryWrapper<OrderInfo>  queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);

        OrderInfo orderInfo= baseMapper.selectOne(queryWrapper);
        // 回调的时候 ,订单有可能被删除了,所有要判断
        if (orderInfo == null){
            return null;
        }
        // 返回订单状态
        return orderInfo.getOrderStatus();
    }

    // 查询创建时间超过minutes分钟并且未支付的订单
    @Override
    public List<OrderInfo> getNoPayOrderByDuration(int minutes) {
        // 时间实例, 5分钟前
        Instant instant = Instant.now().minus(Duration.ofMinutes(minutes));

        QueryWrapper<OrderInfo> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("order_status",OrderStatus.NOTPAY.getType());// 未支付
        queryWrapper.le("create_time",instant); // 创建时间小于5分钟之前

        List<OrderInfo> orderInfoList = baseMapper.selectList(queryWrapper);
        return orderInfoList;
    }

    @Override
    public OrderInfo getOrderByOrderNo(String orderNo) {
        QueryWrapper<OrderInfo> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("order_no",orderNo);

        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }

    /**
     * 根据产品 id 查找已存在但未支付的订单
     *
     * @param productId
     * @return
     */
    private OrderInfo getNoPayOrderByProductId(Integer productId) {
        // 构建查询条件
        QueryWrapper<OrderInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id", productId);//商品id
        queryWrapper.eq("order_status", OrderStatus.NOTPAY.getType());//订单状态
        // queryWrapper.eq("user_id",user_id);//当前登录用户id

        OrderInfo orderInfo = baseMapper.selectOne(queryWrapper);
        return orderInfo;
    }
}
